package com.fxcomponents
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	/** Used to adapt UIComponents to AS projects. Should use Event.RENDER. */
	
	public class Component extends Sprite
	{
		public function Component()
		{
			super();
			
			createChildren();
			commitProperties();
			updateDisplayList(0, 0);
		}
		
		public var measuredWidth:Number = 0; 
		public var measuredHeight:Number = 0;
		
		public function get unscaledWidth():Number
		{
			return measuredWidth;
		}
		
		public function get unscaledHeight():Number
		{
			return measuredHeight;
		}
		
		public function setActualSize(w:Number, h:Number):void
	    {
	    	measuredWidth = w;
	    	measuredHeight = h;
	    	
	    	invalidateProperties();
			invalidateDisplayList();
	    }
		
		public function move(x:Number, y:Number):void
	    {
	        super.x = x;
	        super.y = y;
	    }
		
		override public function set width(value:Number):void
		{
			measuredWidth = value;
			
			invalidateProperties();
			invalidateDisplayList();
		}
		
		override public function set height(value:Number):void
		{
			measuredHeight = value;
			
			invalidateProperties();
			invalidateDisplayList();
		}
		
		protected function createChildren():void
		{
			
		}
		
		protected function commitProperties():void
		{
			
		}
		
		protected function measure():void
	    {
	        measuredWidth = 0;
	        measuredHeight = 0;
	    }
		
		protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			
		}
		
		protected function invalidateProperties():void
		{
			addEventListener(Event.ENTER_FRAME, onInvalidateProperties);
		}
		
		private function onInvalidateProperties(e:Event):void
		{
			removeEventListener(Event.ENTER_FRAME, onInvalidateProperties);
			commitProperties();
		}
		
		protected function invalidateDisplayList():void
		{
			addEventListener(Event.ENTER_FRAME, onInvalidateDisplayList);
		}
		
		private function onInvalidateDisplayList(e:Event):void
		{
			removeEventListener(Event.ENTER_FRAME, onInvalidateDisplayList);
			updateDisplayList(measuredWidth, measuredHeight);
		}
	}
}